package model

import (
	"gorm.io/gorm"
)

type Category struct {
	Model
	Name     string    `json:"name,omitempty"`
	Desc     string    `json:"desc,omitempty"`
	UserID   uint      `json:"user_id,omitempty"`
	Type     int8      `json:"type,omitempty"`
	Pictures []Picture `json:"details,omitempty" gorm:"foreignKey:CategoryID"`
}

func (c Category) TableName() string {
	return "img_category"
}

func (c Category) Create(db *gorm.DB) error {
	return db.Create(&c).Error
}

func (c Category) Update(db *gorm.DB) error {
	return db.Model(&c).Updates(&c).Error
}
func (c Category) Get(db *gorm.DB) (Category, error) {
	var category Category
	err := db.Where(&c).First(&category).Error
	return category, err
}
func (c Category) List(db *gorm.DB, pageOffset int, pageSize int) ([]*Category, error) {
	var categorys []*Category
	err := db.Where(&c).Offset(pageOffset).Limit(pageSize).Find(&categorys).Error
	return categorys, err
}
func (c Category) Delete(db *gorm.DB) error {
	return db.Delete(&c).Error
}
